<template>
	<view class="weather-container">
		<view class="title">明天天气预报</view>
		<view class="weather-info" v-if="hasData">
			<view class="info-item">日期：{{ tomorrowWeather.date }}</view>
			<view class="info-item">星期：{{ tomorrowWeather.week }}</view>
			<view class="info-item">最高温：{{ tomorrowWeather.high }}</view>
			<view class="info-item">最低温：{{ tomorrowWeather.low }}</view>
			<view class="info-item">天气：{{ tomorrowWeather.type }}</view>
		</view>
		<view class="loading" v-else-if="loading">加载中...</view>
		<view class="error" v-else>
			加载失败，请稍后重试<br />
			<text class="error-detail">{{ errorMsg }}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tomorrowWeather: {},
				loading: true,
				hasData: false,
				errorMsg: ''
			};
		},
		onLoad() {
			console.log('页面开始加载，触发天气请求');
			this.getTomorrowWeather();
		},
		methods: {
			getTomorrowWeather() {
				console.log('开始执行getTomorrowWeather函数，发起天气接口请求');
				uni.request({
					url: 'http://t.weather.sojson.com/api/weather/city/101230501',
					method: 'GET',
					success: (res) => {
						console.log('接口请求成功，返回数据：', res);
						if (res.statusCode === 200 && res.data.status === 200) {
							console.log('数据格式验证通过，提取明天天气数据：', res.data.data.forecast[1]);
							this.tomorrowWeather = res.data.data.forecast[1];
							this.hasData = true;
						} else {
							this.errorMsg = '数据格式错误';
							console.error('数据格式错误，返回数据：', res);
						}
						this.loading = false;
					},
					fail: (err) => {
						console.error('接口请求失败，错误信息：', err);
						this.loading = false;
						this.errorMsg = '网络请求失败：' + JSON.stringify(err);
					}
				});
			}
		}
	};
</script>

<style scoped>
	.weather-container {
		padding: 20rpx;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		margin: 30rpx 0;
		text-align: center;
	}

	.weather-info {
		background-color: #f5f5f5;
		padding: 30rpx;
		border-radius: 10rpx;
	}

	.info-item {
		font-size: 30rpx;
		margin: 15rpx 0;
	}

	.loading {
		text-align: center;
		color: #666;
		padding: 50rpx 0;
	}

	.error {
		text-align: center;
		color: #ff4d4f;
		padding: 50rpx 0;
	}

	.error-detail {
		font-size: 24rpx;
		margin-top: 10rpx;
		display: block;
	}
</style>